package com.servicesDirectory.dao.login;

import java.sql.*;

import com.servicesDirectory.common.exception.FunctionalException;
import com.servicesDirectory.common.exception.SystemException;
import com.servicesDirectory.dao.contact.BaseContactDAOImpl;

import java.util.logging.Level;
import java.util.logging.Logger;

public class LoginDAOImpl implements LoginDAO {
	
	
    protected static Connection conn = null;

    protected static void initConnection()
            throws SQLException, ClassNotFoundException {

        if (conn == null) {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:MockPJ_ServiceDirectory", "sa", "12345678");
        }
    }

	@Override
	public boolean checkLogin(String account, String password)
			throws FunctionalException, SystemException {
		// TODO Auto-generated method stub
		conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            /*
        	 * Initialize DB connection
        	 */
            initConnection();
            /*
             * Retrieve data from DB
             */            
            
            String sql = "SELECT Account,Password " +
            		"FROM Users WHERE Account = '" + account +"' AND Password = '" + password + "'";
            

	            stmt = conn.createStatement();
	            rs = stmt.executeQuery(sql);
            if (rs.next()) {
            	return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(BaseContactDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(BaseContactDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }finally {
        	/*
        	 * Close all streams
        	 */
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) {
                    Logger.getLogger(BaseContactDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(BaseContactDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
		return false;
	}

	@Override
	public boolean checkMail(String account, String password)
			throws FunctionalException, SystemException {
		conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            /*
        	 * Initialize DB connection
        	 */
            initConnection();
            /*
             * Retrieve data from DB
             */            
            
            String sql = "SELECT Account,Email " +
            		"FROM Users WHERE Account = '" + account +"' AND Email = '" + password + "'";
            
	            stmt = conn.createStatement();
	            rs = stmt.executeQuery(sql);
            if (rs.next()) {
            	return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(BaseContactDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(BaseContactDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }finally {
        	/*
        	 * Close all streams
        	 */
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) {
                    Logger.getLogger(BaseContactDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(BaseContactDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
		return false;
	}

  
}
